1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.IteratorFeature.MODIFIABLE;
20  import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE;
21  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
22  import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_ADD_WITH_INDEX;
23  import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_SET;
24  import static com.google.common.collect.testing.testers.Platform.listListIteratorTesterNumIterations;
25  import static java.util.Collections.singleton;
26  
27  import com.google.common.annotations.GwtCompatible;
28  import com.google.common.collect.testing.Helpers;
29  import com.google.common.collect.testing.IteratorFeature;
30  import com.google.common.collect.testing.ListIteratorTester;
31  import com.google.common.collect.testing.features.CollectionFeature;
32  import com.google.common.collect.testing.features.ListFeature;
33  
34  import java.util.List;
35  import java.util.ListIterator;
36  import java.util.Set;
37  
38  /**
39   * A generic JUnit test which tests {@code listIterator} operations on a list.
40   * Can't be invoked directly; please see
41   * {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
42   *
43   * @author Chris Povirk
44   * @author Kevin Bourrillion
45   */
46  @GwtCompatible(emulated = true)
47  public class ListListIteratorTester<E> extends AbstractListTester<E> {
48    // TODO: switch to DerivedIteratorTestSuiteBuilder
49  
50    @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
51    @ListFeature.Require(absent = {SUPPORTS_SET, SUPPORTS_ADD_WITH_INDEX})
52    public void testListIterator_unmodifiable() {
53      runListIteratorTest(UNMODIFIABLE);
54    }
55  
56    /*
57     * For now, we don't cope with testing this when the list supports only some
58     * modification operations.
59     */
60    @CollectionFeature.Require(SUPPORTS_REMOVE)
61    @ListFeature.Require({SUPPORTS_SET, SUPPORTS_ADD_WITH_INDEX})
62    public void testListIterator_fullyModifiable() {
63      runListIteratorTest(MODIFIABLE);
64    }
65  
66    private void runListIteratorTest(Set<IteratorFeature> features) {
67      new ListIteratorTester<E>(
68          listListIteratorTesterNumIterations(), singleton(samples.e4), features,
69          Helpers.copyToList(getOrderedElements()), 0) {
70        {
71          // TODO: don't set this universally
72          stopTestingWhenAddThrowsException();
73        }
74  
75        @Override protected ListIterator<E> newTargetIterator() {
76          resetCollection();
77          return getList().listIterator();
78        }
79  
80        @Override protected void verify(List<E> elements) {
81          expectContents(elements);
82        }
83      }.test();
84    }
85  
86    public void testListIterator_tooLow() {
87      try {
88        getList().listIterator(-1);
89        fail();
90      } catch (IndexOutOfBoundsException expected) {
91      }
92    }
93  
94    public void testListIterator_tooHigh() {
95      try {
96        getList().listIterator(getNumElements() + 1);
97        fail();
98      } catch (IndexOutOfBoundsException expected) {
99      }
100   }
101 
102   public void testListIterator_atSize() {
103     getList().listIterator(getNumElements());
104     // TODO: run the iterator through ListIteratorTester
105   }
106 }
107